import { mockAddressList, mockDelay, mockApiResponse } from '@/api/goods/mock'
import http from '@/utils/http'  




// 是否使用模拟数据（开发时使用）
const USE_MOCK_DATA = false // 临时启用模拟数据，避免API错误


/**
 * 获取地址列表
 * @param {Object} params - 查询参数
 */
export async function getAddressList(params = {}) {
  try {
    const result = await http.get('/address/list', params)
    return result
  } catch (error) {
    console.error('获取地址列表失败:', error)
    throw error
  }
}

/**
 * 获取地址详情
 * @param {string|number} addressId - 地址ID
 */
export async function getAddressDetail(addressId) {
  try {
    const result = await http.get(`/address/detail/${addressId}`)
    return result
  } catch (error) {
    console.error('获取地址详情失败:', error)
    throw error
  }
}

/**
 * 添加地址
 * @param {Object} addressData - 地址数据
 */
export async function addAddress(addressData) {
  try {
    const result = await http.post('/address/add', addressData)
    return result
  } catch (error) {
    console.error('添加地址失败:', error)
    throw error
  }
}

/**
 * 更新地址
 * @param {string|number} addressId - 地址ID
 * @param {Object} addressData - 地址数据
 */
export async function updateAddress(addressId, addressData) {
  try {
    const result = await http.put(`/address/update/${addressId}`, addressData)
    return result
  } catch (error) {
    console.error('更新地址失败:', error)
    throw error
  }
}

/**
 * 删除地址
 * @param {string|number} addressId - 地址ID
 */
export async function deleteAddress(addressId) {
  try {
    const result = await http.delete(`/address/delete/${addressId}`)
    return result
  } catch (error) {
    console.error('删除地址失败:', error)
    throw error
  }
}

/**
 * 设置默认地址
 * @param {string|number} addressId - 地址ID
 */
export async function setDefaultAddress(addressId) {
  try {
    const result = await http.put(`/address/set-default/${addressId}`)
    return result
  } catch (error) {
    console.error('设置默认地址失败:', error)
    throw error
  }
}

/**
 * 获取默认地址
 */
export async function getDefaultAddress() {
  try {
    const result = await http.get('/address/default')
    return result
  } catch (error) {
    console.error('获取默认地址失败:', error)
    throw error
  }
}

/**
 * 获取省份列表
 */
export async function getProvinces() {
  try {
    const result = await http.get('/address/provinces')
    return result
  } catch (error) {
    console.error('获取省份列表失败:', error)
    throw error
  }
}

/**
 * 获取城市列表
 * @param {string|number} provinceId - 省份ID
 */
export async function getCities(provinceId) {
  try {
    const result = await http.get(`/address/cities/${provinceId}`)
    return result
  } catch (error) {
    console.error('获取城市列表失败:', error)
    throw error
  }
}

/**
 * 获取区县列表
 * @param {string|number} cityId - 城市ID
 */
export async function getDistricts(cityId) {
  try {
    const result = await http.get(`/address/districts/${cityId}`)
    return result
  } catch (error) {
    console.error('获取区县列表失败:', error)
    throw error
  }
}

/**
 * 验证地址
 * @param {Object} addressData - 地址数据
 */
export async function validateAddress(addressData) {
  try {
    const result = await http.post('/address/validate', addressData)
    return result
  } catch (error) {
    console.error('验证地址失败:', error)
    throw error
  }
}     